package com.example.presentshopping.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.TextView;


import com.example.presentshopping.R;
import com.example.presentshopping.config.bean.CategoryListBean;

import java.util.List;

// 地址选择器
public class MyProvinceAdapter extends BaseAdapter {

    private List<CategoryListBean.DataBean.ListBean> mProvinceDate;
    private Context context;
    private int grouPosition;
    // 父级是否选中。
    private boolean parentSelected = false;

    private OnShengClickListener onShengClickListener;
    private OnShengCheckListener onShengCheckListener;

    // 父级是否选中。
    public void setParentSelected(boolean selected) {
        this.parentSelected = selected;
    }

    public boolean isParentSelected() {
        return parentSelected;
    }

    public void setGrouPosition(int position) {
        this.grouPosition = position;
    }

    // 更新数据。
    public void addALLData(List<CategoryListBean.DataBean.ListBean> mpList) {
        if (mProvinceDate != null && mProvinceDate.size() > 0) {
            mProvinceDate = null;
        }
        this.mProvinceDate = mpList;
        notifyDataSetChanged();
    }

    public MyProvinceAdapter(List<CategoryListBean.DataBean.ListBean> mProvinceDate, Context context) {
        this.mProvinceDate = mProvinceDate;
        this.context = context;
    }

    @Override
    public int getCount() {
        if (mProvinceDate != null && mProvinceDate.size() > 0) {
            return mProvinceDate.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (mProvinceDate != null && mProvinceDate.size() > 0) {
            return mProvinceDate.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ProHodler tag;
        if (convertView == null) {
            tag = new ProHodler();
            convertView = View.inflate(context, R.layout.item_sheng_picker, null);
            tag.textView = convertView.findViewById(R.id.tv_sheng);
            tag.sesebas = convertView.findViewById(R.id.bascid);
            convertView.setTag(tag);
        } else {
            tag = (ProHodler) convertView.getTag();
        }
        if (mProvinceDate != null && mProvinceDate.size() > 0) {
            // 父级是否选中。


            // item是否选择。
            tag.textView.setTextColor(context.getResources().getColor(R.color.colortext));
            tag.sesebas.setVisibility(View.INVISIBLE);
            if (grouPosition == position) {
                tag.textView.setTextColor(context.getResources().getColor(R.color.purple_200));
                tag.sesebas.setVisibility(View.VISIBLE);

            }

            // 设置内容

            final ProHodler finalTag = tag;
            tag.textView.setText(mProvinceDate.get(position).getName());
            // 展开点击
            tag.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onShengClickListener.onShengClick(finalTag.textView, position,
                            mProvinceDate.get(position), mProvinceDate.get(position).getId());


                }
            });
            // 选中点击
        }
        return convertView;
    }

    private static class ProHodler {
        private TextView textView;
        private View sesebas;
    }

    public interface OnShengClickListener {
        void onShengClick(View view, int position, CategoryListBean.DataBean.ListBean mp, int mpList);
    }

    public interface OnShengCheckListener {
        void onShengCheck(boolean isChecked, int position, CategoryListBean.DataBean.ListBean mp, int mpList);
    }

    public void setOnShengClickListener(OnShengClickListener onShengClickListener) {
        this.onShengClickListener = onShengClickListener;
    }

}
